<?php
/**
 * PRequest
 * Class to get arguments in URL and POST requests
 *
 * @author Patrice Ferlet <metal3d@copix.org> 
 * @package core
 * @license http://opensource.org/licenses/lgpl-3.0.html GNU Lesser General Public License Version 3.0 (LGPLv3)
*/


class PRequest {

    static private $_instance=NULL;
    static private $_parameters=array();

    private function __construct(){
        $args = PRequest::checkPrepend();
        PRequest::$_parameters = array_merge($_GET, $_POST,$_REQUEST,$args);
    }


    /**
     * Returns GET and POST values
     *
     * @param string $request_variable
     * @param mixed $default_value
     * @return mixed
     */
    public function get($varName,$default=null){
        if(isset(PRequest::$_parameters[$varName])){
            return PRequest::$_parameters[$varName];
        }
        return $default;
    }


    /**
    * Get instance
    *
    * @return PRequest request_object
    */

    public function instance(){
        if(PRequest::$_instance==NULL){
            PRequest::$_instance = new PRequest();
        }
        return PRequest::$_instance;
    }

    /**
    * Get transcibed URI if no "index.php" was called
    * If we ask for server.com/foo/bar/baz, we are in case of 404 page. We need to 
    * get foo as module, bar as group and baz as action
    */
    private function checkPrepend(){
        $uri = $_SERVER['REQUEST_URI'];
        $arr = array();
        //if($uri != "/" && $uri != "/index.php"){
        if(strpos($uri, '/index.php') != 0){
            $arr = array(
                'module'=>'default',
                'group' => 'default',
                'action' => 'default'
            );
            $uri = substr($uri,1);
            $args = explode("/",$uri);
            if(strlen($args[0])) {
                $arr['module'] = $args[0];
            }
            if(strlen($args[1])) {
                $arr['group'] = $args[1];
            }
            if(strlen($args[2])) {
                $arr['action'] = $args[2];
            }

            //Warning, if 
        }
        return $arr;    
    }

}
